package com.smart.wap.work;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.smart.common.entities.ajax.AjaxResult;
import com.smart.common.entities.constant.SmartConstant;
import com.smart.common.entities.core.Page;
import com.smart.common.entities.file.UploadResult;
import com.smart.common.entities.option.SelectOption;
import com.smart.manager.member.entities.models.MemberMaterialPig;
import com.smart.manager.product.repository.BasicPigstyRepository;
import com.smart.manager.system.repository.DictRepository;
import com.smart.manager.work.entities.assembler.StaffSimple;
import com.smart.manager.work.entities.assembler.WorkOrder;
import com.smart.manager.work.entities.enums.PigOrderKind;
import com.smart.manager.work.entities.enums.PigOrderStatus;
import com.smart.manager.work.entities.models.PigOrder;
import com.smart.manager.work.entities.models.PigOrderFoods;
import com.smart.manager.work.repository.BasicStaffRepository;
import com.smart.manager.work.views.BasicStaffView;
import com.smart.service.FileService;
import com.smart.utils.WorkAcl;
import com.smart.utils.WorkStp;
import com.smart.wap.bodys.RWorkBody;
import com.smart.wap.bodys.WorkOrderResult;
import com.smart.wap.bodys.WorkStaffBody;
import com.smart.wap.service.WorkStaffService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


/**
 * 喂养端账号管理
 * @author leo
 * @date 2023/09/25
 */
@Api(tags = "喂养端-账号管理")
@RestController
@RequestMapping(SmartConstant.WAP_API_PREFIX + "work/staff")
@RequiredArgsConstructor
@Log4j2
public class WorkStaffController {


    private final BasicStaffRepository basicStaffRepository;
    private final WorkStaffService workStaffService;
    private final BasicPigstyRepository basicPigstyRepository;
    private final DictRepository dictRepository;
    private final FileService fileService;

    @ApiOperation(value = "获取个人信息")
    @GetMapping(value = "getInfo")
    @SaCheckLogin(type = WorkStp.TYPE)
    public AjaxResult<StaffSimple> getInfo(){
        StaffSimple info = workStaffService.getInfo(WorkAcl.getLoginId());
        return AjaxResult.data(info);
    }


    @ApiOperation(value = "获取配置文件")
    @GetMapping(value = "getConfig")
    @SaCheckLogin(type = WorkStp.TYPE)
    public AjaxResult<String> getConfig(){
        String config = workStaffService.getConfig(WorkAcl.getLoginId());
        return AjaxResult.data(config);
    }


    @ApiOperation(value = "获取所有员工")
    @GetMapping(value = "getInfoList")
    @SaCheckLogin(type = WorkStp.TYPE)
    public AjaxResult<List<BasicStaffView>> getInfoList(){
        List<BasicStaffView> infoList = workStaffService.getInfoList(WorkAcl.getLoginId());
        return AjaxResult.data(infoList);
    }

    @ApiOperation(value = "添加喂养端账号(管理员功能)")
    @PostMapping(value = "setAccount")
    @SaCheckLogin(type = WorkStp.TYPE)
    public AjaxResult<String> setAccount(@RequestBody @Validated WorkStaffBody.NewBody body){
        workStaffService.setAccount(WorkAcl.getLoginId(),body);
        return AjaxResult.success();
    }

    @ApiOperation(value = "获取我的工单")
    @GetMapping(value = "getOneWorkOder/{kind}")
    @SaCheckLogin(type = WorkStp.TYPE)
    public AjaxResult<Page<PigOrder>> getOneWorkOder(@Validated RWorkBody.SearchBody body){
        Page<PigOrder> oder = workStaffService.getOneWorkOder(body);
        return AjaxResult.data(oder);
    }

    @ApiOperation(value = "获取我的喂食工单(v1)")
    @GetMapping(value = "getOneWorkOderFoods")
    @SaCheckLogin(type = WorkStp.TYPE)
    public AjaxResult<Page<PigOrderFoods>> getOneWorkOderFoods(@Validated RWorkBody.SearchBody body){
        Page<PigOrderFoods> oder = workStaffService.getOneWorkOderFoods(body);
        return AjaxResult.data(oder);
    }


    @ApiOperation(value = "获取已完成的入圈工单")
    @GetMapping(value = "getEnterOder")
    @SaCheckLogin(type = WorkStp.TYPE)
    public AjaxResult<Page<PigOrder>> getEnterOder(@Validated RWorkBody.SearchAllBody body){
        Page<PigOrder> workOder = workStaffService.getOderList(WorkAcl.getLoginId(), PigOrderKind.L1, PigOrderStatus.COMPLETED, body);
        return AjaxResult.data(workOder);
    }
    @ApiOperation(value = "获取已完成的喂食工单(v1)")
    @GetMapping(value = "getFoodOder")
    @SaCheckLogin(type = WorkStp.TYPE)
    public AjaxResult<Page<PigOrderFoods>> getFoodOder(@Validated RWorkBody.SearchAllBody body){
        Page<PigOrderFoods> workOder = workStaffService.getFoodOder(WorkAcl.getLoginId(), PigOrderStatus.COMPLETED,body);
        return AjaxResult.data(workOder);
    }

    @ApiOperation(value = "获取入圈工单(管理员功能)")
    @GetMapping(value = "getWork")
    @SaCheckLogin(type = WorkStp.TYPE)
    public AjaxResult<Page<PigOrder>> getWork(@Validated RWorkBody.SearchAllBody body){
        Page<PigOrder> workOrder = workStaffService.getListWorkOrder(WorkAcl.getLoginId(),body);
        return AjaxResult.data(workOrder);
    }


    @ApiOperation(value = "获取全部工单(管理员功能)")
    @GetMapping(value = "getListOrder")
    @SaCheckLogin(type = WorkStp.TYPE)
    public AjaxResult<Page<PigOrder>> getListOrder(WorkStaffBody.Search body){
        Page<PigOrder> workOrder = workStaffService.getWorkOrder(WorkAcl.getLoginId(),body);
        return AjaxResult.data(workOrder);
    }

    @ApiOperation(value = "派发工单(管理员功能)")
    @PostMapping(value = "distribute")
    @SaCheckLogin(type = WorkStp.TYPE)
    public AjaxResult<String> distribute(@RequestBody @Validated WorkStaffBody.distributeBody body){
        workStaffService.distribute(WorkAcl.getLoginId(),body);
        return AjaxResult.success();
    }

    @ApiOperation(value = "开始录像")
    @GetMapping(value = "feeding/{code}")
    @SaCheckLogin(type = WorkStp.TYPE)
    public AjaxResult<String> feeding(@PathVariable String code){
        workStaffService.videoTape(WorkAcl.getLoginId(),code);
        return AjaxResult.success();
    }

    @ApiOperation(value = "停止录像")
    @GetMapping(value = "stop/{code}")
    @SaCheckLogin(type = WorkStp.TYPE)
    public AjaxResult<String> stop(@PathVariable String code){
        workStaffService.stop(code);
        return AjaxResult.success();
    }

    @ApiOperation(value = "提交入圈工单")
    @PostMapping(value = "addEnter")
    @SaCheckLogin(type = WorkStp.TYPE)
    public AjaxResult<String> addEnter(@RequestBody @Validated WorkStaffBody.addBody body){
        workStaffService.enterTheCircle(WorkAcl.getLoginId(),body);
        return AjaxResult.success();
    }

    @ApiOperation(value = "提交喂养工单(v1)")
    @PostMapping(value = "raiseV1")
    @SaCheckLogin(type = WorkStp.TYPE)
    public AjaxResult<String> raiseV1(@RequestBody @Validated WorkOrder.SubmitBody body){
        workStaffService.raise(WorkAcl.getLoginId(),body);
        return AjaxResult.success();
    }



    @ApiOperation(value = "提交服务工单")
    @PostMapping(value = "service")
    @SaCheckLogin(type = WorkStp.TYPE)
    public AjaxResult<String> service(@RequestBody @Validated WorkOrder.SubmitBody body){
        workStaffService.ministry(WorkAcl.getLoginId(),body);
        return AjaxResult.success();
    }

    @ApiOperation(value = "完成的服务工单")
    @PostMapping(value = "finishService")
    @SaCheckLogin(type = WorkStp.TYPE)
    public AjaxResult<Page<PigOrder>> finishService(@RequestBody RWorkBody.SearchAllBody body){
        Page<PigOrder> orders = workStaffService.getList(WorkAcl.getLoginId(), PigOrderKind.L5,body);
        return AjaxResult.data(orders);
    }

    @ApiOperation(value = "提交治疗工单")
    @PostMapping(value = "treatment")
    @SaCheckLogin(type = WorkStp.TYPE)
    public AjaxResult<String> treatment(@RequestBody @Validated WorkOrder.TreatmentBody body){
        workStaffService.treatment(WorkAcl.getLoginId(),body);
        return AjaxResult.success();
    }

    @ApiOperation(value = "完成的治疗工单")
    @PostMapping(value = "finishTreat")
    @SaCheckLogin(type = WorkStp.TYPE)
    public AjaxResult<Page<PigOrder>> finishTreat(@RequestBody RWorkBody.SearchAllBody body){
        Page<PigOrder> orders = workStaffService.getList(WorkAcl.getLoginId(), PigOrderKind.L4,body);
        return AjaxResult.data(orders);
    }


    @ApiOperation(value = "提交出圈工单")
    @PostMapping(value = "comeOut")
    @SaCheckLogin(type = WorkStp.TYPE)
    public AjaxResult<String> comeOut(@RequestBody @Validated WorkStaffBody.addBody body){
        workStaffService.outOfTheCircle(WorkAcl.getLoginId(),body);
        return AjaxResult.success();
    }

    @ApiOperation(value = "我的角标")
    @GetMapping(value = "getSubscript")
    public AjaxResult<WorkOrderResult> getSubscript(){
        WorkOrderResult result = workStaffService.getWorkOrderHorn(WorkAcl.getLoginId());
        return AjaxResult.data(result);
    }

    @ApiOperation(value = "我的完成")
    @GetMapping(value = "getDetailJob")
    public AjaxResult<WorkOrderResult.Finish> getDetailJob(){
        WorkOrderResult.Finish detailJob = workStaffService.getDetailJob();
        return AjaxResult.data(detailJob);
    }

    @ApiOperation(value = "猪圈数据字典(v2)")
    @GetMapping("basicPigstyOptions/{isOrganic}")
    public AjaxResult<List<SelectOption>> getKindOptions(@PathVariable Boolean isOrganic) {
        List<SelectOption> result = basicPigstyRepository.getCircleListOptions(isOrganic);
        return AjaxResult.data(result);
    }

    @ApiOperation(value = "获取我管理猪仔")
    @GetMapping("getOneList")
    public AjaxResult<List<MemberMaterialPig>> getOneList() {
        List<MemberMaterialPig> oneList = workStaffService.getOneList(WorkAcl.getLoginId());
        return AjaxResult.data(oneList);
    }

    @ApiOperation(value = "管家数据字典" )
    @GetMapping("basicStaffOptions")
    public AjaxResult<List<SelectOption>> basicStaffOptions() {
        List<SelectOption> result = basicStaffRepository.getOptions();
        return AjaxResult.data(result);
    }


    @ApiOperation(value = "治疗分类" )
    @GetMapping("getTreatKind")
    public AjaxResult<List<SelectOption>> getTreatKind() {
        List<SelectOption> result = dictRepository.readDict("TreatKind");
        return AjaxResult.data(result);
    }

    @ApiOperation(value = "根据耳标查询猪仔")
    @GetMapping("getPig/{mac}")
    public AjaxResult<MemberMaterialPig> getPig(@PathVariable String mac) {
        MemberMaterialPig pig = workStaffService.getPig(mac);
        return AjaxResult.data(pig);
    }

    @ApiOperation(value = "小程序上传头像")
    @PostMapping ("setAvatarImg")
    public AjaxResult<String> setAvatarImg(@RequestBody @Validated WorkStaffBody.SetInfo body) {
        workStaffService.setImg(body);
        return AjaxResult.success();
    }


    /**
     * 二进制上传图片
     *
     * @param file 文件
     * @return {@code AjaxResult}
     */
    @PostMapping("form-data-image")
    @ApiOperation(value = "二进制上传图片")
    public AjaxResult<UploadResult> uploadImage(@RequestParam("file") MultipartFile file) {
        UploadResult uploadResult = fileService.uploadImg(file, "", "");
        return AjaxResult.data(uploadResult);

    }


}
